package com.easy.evaluation.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.bean.Evaluation;
import com.easy.common.service.IEvaluationService;
import com.easy.evaluation.mapper.EvaluationMapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EvaluationServiceImpl extends ServiceImpl<EvaluationMapper, Evaluation> implements IEvaluationService {
    @Override
    public Evaluation getEvaluationById(Integer id) {
        return super.getById(id);
    }

    @Override
    public int addEvaluation(Evaluation evaluation) {
        return super.save(evaluation) ? 1 : 0;
    }

    @Override
    public int updateEvaluation(Evaluation evaluation) {
        return super.updateById(evaluation) ? 1 : 0;
    }

    @Override
    public int deleteEvaluation(Integer id) {
        Evaluation evaluation = super.getById(id);
        if (evaluation != null) {
            evaluation.setIsdel(1);
            return super.updateById(evaluation) ? 1 : 0;
        }
        return 0;
    }

    @Override
    public List<Evaluation> getAll(Evaluation evaluation) {
        LambdaQueryWrapper<Evaluation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(evaluation.getContent() != null && !evaluation.getContent().isEmpty(), Evaluation::getContent, evaluation.getContent());
        queryWrapper.eq(evaluation.getScore() != null, Evaluation::getScore, evaluation.getScore());
        queryWrapper.eq(evaluation.getOrder_id() != null, Evaluation::getOrder_id, evaluation.getOrder_id());
        queryWrapper.eq(evaluation.getUser_id() != null, Evaluation::getUser_id, evaluation.getUser_id());
        queryWrapper.eq(evaluation.getDriver_id() != null, Evaluation::getDriver_id, evaluation.getDriver_id());
        queryWrapper.eq(Evaluation::getIsdel, 0);
        return super.list(queryWrapper);
    }

    @Override
    public IPage<Evaluation> getPage(Evaluation evaluation, IPage page) {
        LambdaQueryWrapper<Evaluation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(evaluation.getContent() != null && !evaluation.getContent().isEmpty(), Evaluation::getContent, evaluation.getContent());
        queryWrapper.eq(evaluation.getScore() != null, Evaluation::getScore, evaluation.getScore());
        queryWrapper.eq(evaluation.getOrder_id() != null, Evaluation::getOrder_id, evaluation.getOrder_id());
        queryWrapper.eq(evaluation.getUser_id() != null, Evaluation::getUser_id, evaluation.getUser_id());
        queryWrapper.eq(evaluation.getDriver_id() != null, Evaluation::getDriver_id, evaluation.getDriver_id());
        queryWrapper.eq(Evaluation::getIsdel, 0);
        return super.page(page, queryWrapper);
    }
}
